/*
 * DataSet
 * 
 * Project: Search Engine
 * 
 * Copy right 2012 by Phan Hoàn Vũ
 * Email hoanvuphan@gmail.com
 */

package hcmus.fit.ml.tr.data;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * @author hvp
 * 
 */
public class Instances {
    private String relation;
    private List<Attribute> attributes;
    private List<Instance> instances;
    private Attribute classAttribute;

    public Instances(String relation) {
        this.relation = relation;
        this.attributes = new ArrayList<Attribute>();
        this.instances = new ArrayList<Instance>();
    }

    public Instances() {
        this(null);
    }

    public void addAttribute(String name, String clazzInfo) {
        String upperCaseName = name.toUpperCase();
        // Is enumeration type
        if (StringUtils.startsWith(clazzInfo, "{")) {
            this.attributes.add(new EnumAttribute(upperCaseName, clazzInfo));
        } else {
            // TODO: [HVP] Fix support type
            this.attributes.add(new NumbericAttribute(upperCaseName, clazzInfo));
        }
    }

    public void addRow(String[] rowData) {
        Instance r = new Instance();
        for (int i = 0; i < attributes.size(); i++) {
            Attribute attr = attributes.get(i);
            r.addAttribute(attributes.get(i).getName(), attr.parseValue(rowData[i].trim()));
        }
        this.instances.add(r);
    }

    public List<Instance> getInstances() {
        return instances;
    }

    public String getRelation() {
        return relation;
    }

    public EnumAttribute getClassAttribute() {
        if(this.classAttribute == null) {
            this.classAttribute = attributes.get(attributes.size() - 1);
        }
        return (EnumAttribute) classAttribute;
    }

    public List<Attribute> getAttributes() {
        return attributes;
    }

    public void setAttributes(List<Attribute> attributes) {
        this.attributes = attributes;
    }

    public void setInstances(List<Instance> instances) {
        this.instances = instances;
    }
}
